﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TD.TheGame
{
    public class CastleShooter : Sprite
    {
        public CastleShooterArrow[] CastleArrows
        {
            get { return arrows; }
        }

        CastleShooterArrow[] arrows = new CastleShooterArrow[20];
        Random random = new Random();
        Rectangle[] shoot;
        Rectangle[] normal;
        private int intervalTime;
        private int currentTime;
        private int currentArrow;
        public CastleShooter(Game game, Vector2 position, bool active, int maxLoopTime, SpriteEnumState state, SpriteEnumState side)
            : base(game, position, active, maxLoopTime, state)
        {
            //Initialize Arrows for 
            for (int i = 0; i < arrows.Count(); i++)
            {
                CastleShooterArrow arrow = new CastleShooterArrow(game,
                    new Vector2(0, 0),
                    false,
                    500,
                    random.Next(4, 7),
                    side);
                arrows[i] = arrow;
            }
            //arrow = new CastleShooterArrow(game, new Vector2(0, 500), true, 300, 5);
            spriteEffects = SpriteEffects.FlipHorizontally;
            shoot = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "CastleShooter\\" + "castlesupportShoot.xml");
            normal =  animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "CastleShooter\\" + "castlesupportNormal.xml");
            current = normal;

            //set intervalTime
            intervalTime = 4000;
        }


        protected override Microsoft.Xna.Framework.Graphics.Texture2D getTexture()
        {
            //Init texture
            manager.LoadAsset("level\\castle\\castleShooter");
            texture = manager.UseAsset("level\\castle\\castleShooter");
            return texture;
        }

        public override void Update(GameTime gameTime)
        {
            if (oldState != State)
            {
                if (State == SpriteEnumState.Normal)
                {
                    current = normal;
                }
                if (State == SpriteEnumState.Shoot)
                {
                    current = shoot;
                }
                //set the currentsprite different
                currentRect = 0;
            }
            oldState = State;
            Shoot(gameTime);
            //update Arrows
            foreach (var arrow in arrows)
            {
                if (arrow.Active)
                {
                    arrow.Update(gameTime);
                }            
            }
            base.Update(gameTime);
        }


        public void Shoot(GameTime gameTime)
        {
            currentTime += gameTime.ElapsedGameTime.Milliseconds;

            if (currentTime > intervalTime)
            {
                currentTime = 0;
                State = SpriteEnumState.Shoot;
            }
            if (State == SpriteEnumState.Shoot && endSprite)
            {
                State = SpriteEnumState.Normal;
                //InitArrow
                InitArrow();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            //arrow.Draw(gameTime);
            foreach (var arrow in arrows)
            {
                if (arrow.Active)
                {
                    arrow.Draw(gameTime);
                }
            }
            base.Draw(gameTime);
        }

        /// <summary>
        /// Initialize the Arrow
        /// </summary>
        private void InitArrow()
        {
            if (currentArrow >= arrows.Count())
            {
                currentArrow = 0;
            }
            if (arrows[currentArrow].Active == false)
            {
                arrows[currentArrow].position.X = BoundingRectangle.X + BoundingRectangle.Width - 5;
                arrows[currentArrow].position.Y = BoundingRectangle.Y + BoundingRectangle.Height /2 - 30;
                arrows[currentArrow].Active = true;
                currentArrow++;
            }
        }

        public override void Reset()
        {
            throw new NotImplementedException();
        }
    }
}
